package com.enjia.common.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.enjia.common.utils.CommonUtils;
import com.enjia.enjiashop.MyApplication;
import com.enjia.enjiashop.R;

/**
 * 提示框
 * Created by feifei on 2017/9/23.
 * 使用示例
 *
 *      PromptDialog promit = new PromptDialog(mContext);
        promit.setPromptContent("试试","左","右");
        promit.setOnButtonClickListener(new PromptDialog.ButtonClickListener() {
        @Override
        public void leftClick() {
        ToastUtil.showMessage(mContext,"取消");
        }
        @Override
        public void rightClick() {
        ToastUtil.showMessage(mContext,"确定");
        }
        });
        promit.show();
 */

public class PromptDialog extends Dialog implements View.OnClickListener{

    private ButtonClickListener clickListener;

    private Button btn_left;
    private Button btn_right;
    private TextView promptMessage;

    private String contentMessage;
    private String leftContent;
    private String rightContent;

    public PromptDialog(@NonNull Context context) {
        super(context, R.style.PaiLeDialog);
    }

    public PromptDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, R.style.PaiLeDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_base_error);
        this.getWindow().getAttributes().gravity= Gravity.CENTER;
        initView();
    }
    //初始化属性
    private void initView(){
        btn_left = (Button) this.findViewById(R.id.btn_left);
        btn_right = (Button) this.findViewById(R.id.btn_right);
        promptMessage = (TextView) this.findViewById(R.id.dialog_prompt_message);


        btn_left.setOnClickListener(this);
        btn_right.setOnClickListener(this);

        if(null != contentMessage){
            promptMessage.setText(contentMessage);
        }
        if(null != leftContent){
            btn_left.setText(leftContent);
        }
        if(null != rightContent){
            btn_right.setText(rightContent);
        }
        CommonUtils.setDialogSize(MyApplication.context,this,0.8);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //左侧按钮点击事件
            case R.id.btn_left:
                clickListener.leftClick();
                dismiss();
                break;
            //右侧按钮点击事件
            case R.id.btn_right:
                clickListener.rightClick();
                dismiss();
                break;
        }
    }
    //为Dialog设置内容
    public void setPromptContent(String message,String leftContent,String rightContent){
        this.contentMessage = message;
        this.leftContent = leftContent;
        this.rightContent = rightContent;
    }

    public interface ButtonClickListener{
        void leftClick();
        void rightClick();
    }

    public void setOnButtonClickListener(ButtonClickListener clickListener){
        this.clickListener = clickListener;
    }
}
